{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分析提取算子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[31mERROR: usage: ipykernel_launcher.py [options] [file_or_dir] [file_or_dir] [...]\n",
      "ipykernel_launcher.py: error: unrecognized arguments: --f=/home/ai/.local/share/jupyter/runtime/kernel-v399c99730476352b7b4b0c36dfc978d956b870aea.json\n",
      "  inifile: /media/pc/data/lxw/ai/tvm-book/pyproject.toml\n",
      "  rootdir: /media/pc/data/lxw/ai/tvm-book\n",
      "\u001b[0m\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "4",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/media/pc/data/lxw/envs/anaconda3a/envs/ai/lib/python3.12/site-packages/IPython/core/interactiveshell.py:3585: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "import pytest\n",
    "import tvm\n",
    "from tvm import relay\n",
    "from tvm.relay.testing.resnet import get_workload\n",
    "from tvm.relay.testing import run_opt_pass\n",
    "\n",
    "\n",
    "def get_conv_net():\n",
    "    \"\"\"This gets the net for:\n",
    "          conv2d\n",
    "          /  |\n",
    "         /   |\n",
    "    conv2d   |\n",
    "        \\    |\n",
    "         \\   |\n",
    "        elemwise add\n",
    "             |\n",
    "    \"\"\"\n",
    "    dshape = (1, 1, 5, 1)\n",
    "    x = relay.var(\"x\", shape=dshape)\n",
    "    y = relay.nn.conv2d(x, relay.var(\"w1\"), kernel_size=(3, 3), padding=(1, 1), channels=1)\n",
    "    x1 = relay.nn.conv2d(y, relay.var(\"w2\"), kernel_size=(3, 3), padding=(1, 1), channels=1)\n",
    "\n",
    "    z = relay.add(y, x1)\n",
    "\n",
    "    return tvm.IRModule.from_expr(z)\n",
    "\n",
    "\n",
    "def get_conv2d():\n",
    "    x = relay.var(\"x\", shape=(1, 56, 56, 64))\n",
    "    weight1 = relay.var(\"weight1\", shape=(3, 3, 64, 32))\n",
    "    y = relay.nn.conv2d(\n",
    "        x,\n",
    "        weight1,\n",
    "        channels=32,\n",
    "        kernel_size=(3, 3),\n",
    "        padding=(1, 1),\n",
    "        data_layout=\"NHWC\",\n",
    "        kernel_layout=\"HWIO\",\n",
    "    )\n",
    "    return tvm.IRModule.from_expr(y)\n",
    "\n",
    "\n",
    "def test_extract_identity():\n",
    "    mod = get_conv2d()\n",
    "    op_freqs = relay.analysis.list_op_freqs(mod)\n",
    "    assert len(op_freqs) == 1\n",
    "    assert op_freqs[\"nn.conv2d\"] == 1\n",
    "\n",
    "\n",
    "def test_extract_conv_net():\n",
    "    mod = get_conv_net()\n",
    "    op_freqs = relay.analysis.list_op_freqs(mod)\n",
    "    assert len(op_freqs) == 2\n",
    "    assert op_freqs[\"add\"] == 1\n",
    "    assert op_freqs[\"nn.conv2d\"] == 2\n",
    "\n",
    "\n",
    "def test_extract_fused():\n",
    "    mod = get_conv_net()\n",
    "    mod = relay.transform.InferType()(mod)\n",
    "    mod = relay.transform.FuseOps(3)(mod)\n",
    "\n",
    "    op_freqs = relay.analysis.list_op_freqs(mod)\n",
    "    assert len(op_freqs) == 2\n",
    "    assert op_freqs[\"add\"] == 1\n",
    "    assert op_freqs[\"nn.conv2d\"] == 2\n",
    "\n",
    "\n",
    "def test_extract_resnet():\n",
    "    mod, _params = get_workload()\n",
    "    expected_op_freqs = {\n",
    "        \"nn.batch_norm\": 19,\n",
    "        \"nn.conv2d\": 21,\n",
    "        \"nn.relu\": 18,\n",
    "        \"nn.max_pool2d\": 1,\n",
    "        \"add\": 8,\n",
    "        \"nn.global_avg_pool2d\": 1,\n",
    "        \"nn.batch_flatten\": 1,\n",
    "        \"nn.dense\": 1,\n",
    "        \"nn.bias_add\": 1,\n",
    "        \"nn.softmax\": 1,\n",
    "    }\n",
    "    op_freqs = relay.analysis.list_op_freqs(mod)\n",
    "    assert len(op_freqs) == len(expected_op_freqs)\n",
    "    assert all([op_freqs[op] == expected_op_freqs[op] for op in expected_op_freqs])\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    tvm.testing.main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
